
package com.openfeint.internal.request;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.json.JSONException;
import org.json.JSONObject;

import com.openfeint.internal.OpenFeintInternal;
import com.openfeint.internal.Util;
import com.openfeint.internal.request.multipart.FilePart;
import com.openfeint.internal.request.multipart.MultipartHttpEntity;
import com.openfeint.internal.request.multipart.Part;
import com.openfeint.internal.request.multipart.PartSource;
import com.openfeint.internal.request.multipart.StringPart;
import com.openfeint.internal.resource.BlobUploadParameters;

public class BlobPostRequest extends BaseRequest {

    @Override
    public boolean signed() {
        return false;
    }

    BlobUploadParameters mParams;

    protected PartSource mPartSource;

    String mContentType;

    String xpApiPath;

    @Override
    public String method() {
        return "POST";
    }

    // Since blob posts go to another server, you have to overload url as well
    // as path.
    // public String url() {
    // return OpenFeintInternal.getInstance().getServerUrl()+xpApiPath;
    // }
    //
    public void sign() {
        // lol nop
    }

    public String path() {
        return this.xpApiPath;
    }

    protected HttpUriRequest generateRequest() {
        if (null == mPartSource) {
            // This will generate a servercode 0 failure.
            return null;
        }
        HttpPost retval = new HttpPost(url());
        int idx = 0;
        Part parts[] = new Part[1];
        parts[idx++] = new FilePart("avatar", mPartSource, mContentType, null);
        HttpEntity e = new MultipartHttpEntity(parts);
        retval.setEntity(e);
        addParams(retval);
        return retval;
    }

    private IRawRequestDelegate mDelegate;

    public BlobPostRequest(String xpApiPath, PartSource partSource, String contentType) {
        super(null);
        this.xpApiPath = xpApiPath;
        mPartSource = partSource;
        mContentType = contentType;
    }

    public BlobPostRequest(BlobUploadParameters params, PartSource partSource, String contentType) {
        super(null);

        mParams = params;
        mPartSource = partSource;
        mContentType = contentType;
    }

    private static final String CONTENT_TYPE = "application/json";

    public static final String DESIRED_RESPONSE_PREFIX = CONTENT_TYPE + ";";

    protected boolean isResponseJSON() {
        String responseType = getResponseType();
        return responseType != null && responseType.startsWith(DESIRED_RESPONSE_PREFIX);
    }

    @Override
    public void onResponse(int responseCode, byte[] body) {

        if (mDelegate != null) {
            if (isResponseJSON()) {
                String jsonStr = new String(body);
                try {
                    JSONObject js = new JSONObject(jsonStr);
                    String url = js.getString("avatar");
                    mDelegate.onResponse(responseCode, url);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }

    public void setDelegate(IRawRequestDelegate mDelegate) {
        this.mDelegate = mDelegate;
    }
}
